﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandLib
{
    /// <summary>
    /// 文件操作
    /// </summary>
    public class FileHelper
    {
        /// <summary>
        /// 文件转换成Base64字符串
        /// </summary>
        /// <param name="fileName">文件绝对路径</param>
        /// <returns></returns>
        public static String FileToBase64(string fileName)
        {
            string strRet = null;

            try
            {
                using (var fs = new FileStream(fileName, FileMode.Open))
                {
                    byte[] bt = new byte[fs.Length];
                    fs.Read(bt, 0, bt.Length);
                    strRet = Convert.ToBase64String(bt);
                }

            }
            catch (Exception)
            {
                throw;
            }

            return strRet;
        }
        /// <summary>
        /// Base64字符串转文件并保存
        /// </summary>
        /// <param name="base64String">base64字符串</param>
        /// <param name="fileName">保存的文件名</param>
        /// <returns>是否转换并保存成功</returns>
        public static bool Base64ToFile(string base64String, string fileName)
        {
            bool opResult = false;
            try
            {
                using (var stream = new MemoryStream(Convert.FromBase64String(base64String)))
                {
                    FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                    byte[] b = stream.ToArray();
                    fs.Write(b, 0, b.Length);
                    fs.Close();
                }
                opResult = true;
            }
            catch (Exception)
            {
                throw;
            }
            return opResult;
        }
        /// <summary>
        /// 文件读取
        /// </summary>
        /// <param name="root">路径</param>
        /// <returns></returns>
        public static string ReadFile(string root, Encoding encoding)
        {
            var result = string.Empty;
            if (File.Exists(root))
            {
                try
                {
                    result = File.ReadAllText(root, encoding);
                }
                catch (Exception)
                {

                    throw;
                }

            }
            return result;

        }
        /// <summary>
        /// 文件写入
        /// </summary>
        /// <param name="root">文件路径</param>
        /// <param name="text">内容</param>
        /// <param name="encoding">编码</param>
        public static void WriteFile(string root, string text, Encoding encoding)
        {


            try
            {
                File.WriteAllText(root, text, encoding);

            }
            catch (Exception)
            {

                throw;
            }


        }

        /// <summary>
        /// 共享读取
        /// </summary>
        /// <param name="root"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static async Task<string> ReadFileShare(string root, Encoding encoding)
        {
            string result = string.Empty;
            try
            {
                using (var fs = File.Open(root, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    using (var sw = new StreamReader(fs, encoding))
                    {
                        result = await sw.ReadToEndAsync();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
           
            return result;
        }
        /// <summary>
        /// 共享写入
        /// </summary>
        /// <param name="root"></param>
        /// <param name="context"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static async Task WiterFileShare(string root, string context, Encoding encoding)
        {
            try
            {
                using (var fs = File.Open(root, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    using (var sw = new StreamWriter(fs, encoding))
                    {
                        await sw.WriteAsync(context);
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
           

        }
        /// <summary>
        /// 共享读取
        /// </summary>
        /// <param name="root"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string[] ReadFileLines(string root, Encoding encoding)
        {
            var result = new List<string>();
            using (var fs = File.Open(root, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (var sw = new StreamReader(fs, encoding))
                {
                    string Line;
                    while ((Line = sw.ReadLine())!=null)
                    {
                        result.Add(Line);
                    }
                }
            }
            return result.ToArray();
        }

    }
}
